import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import { CreateLog, Roles } from "@/common/decorators";
import { Role } from "@/common/enum/role.enum";
import { RequiredPipe } from "@/common/pipes/required.pipe";
import { SysDept } from "@/database/entity/SysDept";
import { DeptService } from "./dept.service";
import { CreateDeptDto } from "./dto/create-dept.dto";
import { QueryDeptByNameDto } from "./dto/query-dept.dto";
import { UpdateDeptDto } from "./dto/update-dept.dto";

@Controller("sys/dept")
export class DeptController {
    constructor(private deptService: DeptService) {}

    /** 新增系统部门 */
    @CreateLog({ module: "系统部门", type: "create", action: "创建系统部门" }, SysDept)
    @Roles(Role.SUPER_ADMIN)
    @Post("create")
    create(@Body() data: CreateDeptDto) {
        return this.deptService.create(data);
    }

    /** 删除系统部门 */
    @CreateLog({ module: "系统部门", type: "delete", action: "删除系统部门" }, SysDept)
    @Roles(Role.SUPER_ADMIN)
    @Post("remove")
    remove(@Body("id", RequiredPipe) id: string) {
        return this.deptService.remove(id);
    }

    /** 更新系统部门 */
    @CreateLog({ module: "系统部门", type: "update", action: "更新系统部门" }, SysDept)
    @Roles(Role.SUPER_ADMIN)
    @Post("update")
    update(@Body() data: UpdateDeptDto) {
        return this.deptService.update(data);
    }

    /** 查询系统部门 */
    @Get("findById")
    findById(@Query("id", RequiredPipe) id: string) {
        return this.deptService.findById(id);
    }

    /** 获取系统子部门 */
    @Get("listChildren")
    listChildren(@Query("parentId", RequiredPipe) parentId: string) {
        return this.deptService.listChildren(parentId);
    }

    /** 获取指定系统部门的所有部门, 并返回树形结构 */
    @Get("listTree")
    listTree(@Query("code") code: string | null) {
        return this.deptService.listTree(code);
    }

    /** 根据名称查询查询某部门下面的系统部门 */
    @Get("listByName")
    listByName(@Query() data: QueryDeptByNameDto) {
        return this.deptService.listByName(data);
    }
}
